package com.thtf.openguess.util;

import com.google.common.base.Stopwatch;
import com.thtf.openguess.service.AsyncService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.CountDownLatch;

public class Util {

//    public void importFile() throws InterruptedException {

//        String prefix = "INSERT INTO c2016_000000_b204_1_1_test (bgq,dcdxxtm,zzjgdm) VALUES ";
//        StringBuffer suffix = new StringBuffer();
//        String str = "";
//        StringBuilder valuesSql = new StringBuilder();
//        int size = 10000;
//        int bacthSize = 100;
//        //读文件
//        CountDownLatch countDownLatch = new CountDownLatch(bacthSize);
//        Stopwatch started = Stopwatch.createStarted();
//        // 外层循环，总提交事务次数
//        long start = System.currentTimeMillis();
//        for (int i = 1; i <= bacthSize; i++) {
//            suffix = new StringBuffer();
//            // 第j次提交步长
//            for (int j = 1; j <= size; j++) {
//                // 构建SQL后缀
//                suffix.append("('" + UUID.randomUUID().toString() + "','123456'" + ",'123456'" + "),");
//            }
//            // 构建完整SQL
//            String sql = prefix + suffix.substring(0, suffix.length() - 1);
//            // 添加执行SQL
//            asyncService.dealAdd(sql, countDownLatch, size, jdbcTemplate);
//
//            // 清空上一次添加的数据
//            suffix = new StringBuffer();
//        }
//        countDownLatch.await();
//        // 开始时间
//        Long end = System.currentTimeMillis();
//        System.out.println("100万条数据插入花费时间 : " + (end - start) / 1000 + " s" + "  插入完成");
//    }
    public static Integer ramdom(){
        return new Random().nextInt(9999);
    }

     public static <E> E forEachSetValue(E target) {
        if (target == null) {
            return target;
        }
        List<Field> fields = new ArrayList<>();
        try {
            // 1.解析出所有的属性
            Field[] declaredFields = target.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                fields.add(declaredField);
            }
            // 2.把每个属性放入一个集合中
            if (fields.size() <= 0) {
                return target;
            }
//            while (fields.get(0).get(target) != null) {
//                fields.remove(0);
//            }
            for (Field field : fields) {//分别判断字段类型赋随机值
                if (field.getType().getTypeName() == Integer.class.getTypeName()) {
                    field.set(target, ramdom());
                }
                if (field.getType().getTypeName() == String.class.getTypeName()) {
                    field.set(target, ramdom().toString());
                }
                if (field.getType().getTypeName() == Date.class.getTypeName()) {
                    field.set(target, new Date());
                }
            }


        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return target;
    }


}
